package com.example.l3.ktm.GridView;

import java.io.File;
import java.util.List;

import com.example.l3.ktm.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class GridViewAdapter extends BaseAdapter {

	public File[] listFiles;
	private Context context;
	private ViewHolder view;
	
	public GridViewAdapter(Context context, File[] files) {
		listFiles = files;
		this.context = context;
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return listFiles.length;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return listFiles[position];
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		if(convertView == null) {
			view = new ViewHolder();
			convertView = inflater.inflate(R.layout.gridview_cell, null);
			
			view.thumbnailImage = (ImageView) convertView.findViewById(R.id.imgThumb);
			view.thumbName = (TextView) convertView.findViewById(R.id.txtThumbName);
			
			convertView.setTag(view);
		} else {
			view = (ViewHolder) convertView.getTag();
		}
		
		if(listFiles != null) {
			File file = (File) listFiles[position];
			view.thumbName.setText(file.getName());
			if(file != null && file.canRead() && file.exists() && !file.isHidden()) {
				
				if(file.isDirectory()) {
					if(file.getName().equals("Compressed")) {
						view.thumbnailImage.setImageResource(R.drawable.folder_library);
					} else if(file.getName().equals("Document")) {
						view.thumbnailImage.setImageResource(R.drawable.folder_documents);
					} else if(file.getName().equals("Music")) {
						view.thumbnailImage.setImageResource(R.drawable.folder_music);				
					} else if(file.getName().equals("Program")) {
						view.thumbnailImage.setImageResource(R.drawable.folder_applications);
					} else if(file.getName().equals("Video")) {
						view.thumbnailImage.setImageResource(R.drawable.folder_movies);
					} else if(file.getName().equals("Picture")) {
						view.thumbnailImage.setImageResource(R.drawable.folder_photos);
					} else {
						view.thumbnailImage.setImageResource(R.drawable.folder);
					}
				} else if(file.isFile()) {
					CheckFileType(file.getName());
				}
			}
		}
		return convertView;
	}
	
	public static class ViewHolder {
		public ImageView thumbnailImage;
		public TextView thumbName;
	}
	
	public String getExtensionFile(String filename){
		String[] arrayComponents = filename.split("\\.");
		String extensionName = arrayComponents[arrayComponents.length - 1];
		return extensionName;
	}
	
	public void CheckFileType(String fileName) {
		String extensionName = getExtensionFile(fileName);
		if (extensionName.equals("zip")) {//file
			view.thumbnailImage.setImageResource(R.drawable.file_zip);
		} else if(extensionName.equals("rar")) {
			view.thumbnailImage.setImageResource(R.drawable.file_rar);
		} else if(extensionName.equals("doc")) {
			view.thumbnailImage.setImageResource(R.drawable.file_doc);
		} else if(extensionName.equals("docx")) {
			view.thumbnailImage.setImageResource(R.drawable.file_docx);
		} else if(extensionName.equals("ppt")) {
			view.thumbnailImage.setImageResource(R.drawable.file_ppt);
		} else if(extensionName.equals("pptx")) {
			view.thumbnailImage.setImageResource(R.drawable.file_pptx);
		} else if(extensionName.equals("xls")) {
			view.thumbnailImage.setImageResource(R.drawable.file_xls);
		} else if(extensionName.equals("xlsx")) {
			view.thumbnailImage.setImageResource(R.drawable.file_xlsx);
		} else if(extensionName.equals("pdf")) {
			view.thumbnailImage.setImageResource(R.drawable.file_pdf);
		} else if(extensionName.equals("txt")) {
			view.thumbnailImage.setImageResource(R.drawable.file_text);
		} else if(extensionName.equals("mp3")) {
			view.thumbnailImage.setImageResource(R.drawable.file_mp3);
		} else if(extensionName.equals("wav")) {
			view.thumbnailImage.setImageResource(R.drawable.file_wav);
		} else if(extensionName.equals("wma")) {
			view.thumbnailImage.setImageResource(R.drawable.file_wma);
		} else if(extensionName.equals("midi") || extensionName.equals("mid")) {
			view.thumbnailImage.setImageResource(R.drawable.file_midi);
		} else if(extensionName.equals("avi")) {
			view.thumbnailImage.setImageResource(R.drawable.file_avi);
		} else if(extensionName.equals("mpeg")) {
			view.thumbnailImage.setImageResource(R.drawable.file_mpeg);
		} else if(extensionName.equals("mov")) {
			view.thumbnailImage.setImageResource(R.drawable.file_mov);
		} else if(extensionName.equals("wmv")) {
			view.thumbnailImage.setImageResource(R.drawable.file_wmv);
		} else if(extensionName.equals("jpeg") || extensionName.equals("jpg")) {
			view.thumbnailImage.setImageResource(R.drawable.file_jpeg);
		} else if(extensionName.equals("png")) {
			view.thumbnailImage.setImageResource(R.drawable.file_png);
		} else if(extensionName.equals("bmp")) {
			view.thumbnailImage.setImageResource(R.drawable.file_bmp);
		} else if(extensionName.equals("exe") || extensionName.equals("bat")) {
			view.thumbnailImage.setImageResource(R.drawable.file_ini);
		} else if(extensionName.equals("apk")){
			view.thumbnailImage.setImageResource(R.drawable.ic_launcher);
		} else {
			view.thumbnailImage.setImageResource(R.drawable.file_generic);
		}
	}
}
